import router from "@/router";
import { TUILogin } from "@tencentcloud/tui-core";
import axios from "axios";
import { debounce } from "lodash-es";
import qs from "qs";
import { Toast, TOAST_TYPE } from "../TUIKit/components/common/Toast/index";
// export const baseURL = 'https://api.niuyuwang.com'
export const baseURL = "https://demo-api.niuyuwang.com";
const ENV = "hr";

const handleLogout = debounce(() => {
  Toast({
    message: "登录状态已过期,请重新登录",
    type: TOAST_TYPE.ERROR,
  });
  router.replace({ name: "login" });
  localStorage.removeItem("TUIKit-userInfo");
  localStorage.removeItem("userInfo");
  localStorage.removeItem("token");
  localStorage.removeItem("sign");
  TUILogin.logout();
}, 300);

const handleError = debounce((error: any) => {
  Toast({
    message: error,
    type: TOAST_TYPE.ERROR,
  });
}, 300);

const instance = axios.create({
  baseURL: `${baseURL}/`,
  headers: { "content-type": "application/x-www-form-urlencoded" },
  timeout: 30000,
});
instance.interceptors.request.use(
  (config) => {
    // 是否需要设置 token请根据实际情况自行修改
    const isToken = (config.headers || {}).isToken === false;
    let token = localStorage.getItem("token");
    if (token && !isToken) {
      config.headers["token"] = token;
    }

    return config;
  },
  (error) => {
    console.log(error);
    Promise.reject(error);
  }
);
instance.interceptors.response.use(
  (res) => {
    // 未设置状态码则默认成功状态
    let code = res.data.code || 1;
    if (res.data.code === 0) code = 0;
    const msg = res.data.msg;

    if (code !== 1) {
      if (code === 4001) {
        handleLogout();
        Promise.reject("令牌验证失败");
        // TUILogin.logout()
      } else {
        // Toast({
        //   message: msg,
        //   type: TOAST_TYPE.ERROR,
        // });
        handleError(msg);
      }
      return Promise.reject(new Error(msg));
    } else {
      return res.data;
    }
  },
  (error) => {
    console.log(error, ":[Log error] utils request");
    let { message } = error;
    if (message == "Network Error") {
      message = "系统错误";
    } else if (message.includes("timeout")) {
      message = "系统错误请求超时";
    } else if (message.includes("Request failed with status code")) {
      message = "系统错误" + message.substr(message.length - 3) + "异常";
    }
    return Promise.reject(error);
  }
);

export async function GetHomeData() {
  const options = buildOptions("", "hr/company/index", "GET");
  return instance(options);
}

export async function GetHomeToDo() {
  const options = buildOptions("", "hr/company/nodo", "GET");
  return instance(options);
}

export async function GetHomeToDoList(data: any) {
  const options = buildOptions(data, "hr/company/nodo_list", "GET");
  return instance(options);
}

export async function GetHomeLegal() {
  const options = buildOptions("", "hr/company/legal", "GET");
  return instance(options);
}

export async function GetHomeWorkOrder(data: any) {
  const options = buildOptions(data, "hr/company/work_order", "GET");
  return instance(options);
}

export async function GetHomeWorkOrderDetail(data: any) {
  const options = buildOptions(data, "hr/company/work_order_detail", "GET");
  return instance(options);
}

export async function getSmsVerifyCode(data: { appId: any }) {
  const options = buildOptions(
    data,
    "/base/v1/auth_users/user_verify_by_picture",
    "GET"
  );
  return instance(options);
}

export async function loginSystemByVerifyCode(loginInfo: any) {
  const options = buildOptions(
    loginInfo,
    "/base/v1/auth_users/user_login_code"
  );
  return instance(options);
}

export async function loginSystemByToken(data: any) {
  const options = buildOptions(data, "/base/v1/auth_users/user_login_token");
  return instance(options);
}

export async function cancellation(data: any) {
  const options = buildOptions(data, "/base/v1/auth_users/user_delete");
  return instance(options);
}

export async function loginSystem(data: any) {
  const options = buildOptions(data, "hr/login/login");
  return instance(options);
}

export async function getArea() {
  const options = buildOptions("", "hr/index/area");
  return instance(options);
}

export async function userAuth(data: any) {
  const options = buildOptions(data, "hr/user/re_auth");
  return instance(options);
}

export async function getGroupInfo(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/group");
  return instance(options);
}

export async function getLegalList(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/legal_list");
  return instance(options);
}

export async function getUserlList(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/user_list");
  return instance(options);
}

export async function AddMember(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/add_group_member");
  return instance(options);
}

export async function DelMember(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/del_group_member");
  return instance(options);
}

export async function getTagList() {
  const options = buildOptions("", "legal/im.ChatGroup/note_list");
  return instance(options);
}

export async function addTag(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/edit_note");
  return instance(options);
}

export async function getGroupTag(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/get_note");
  return instance(options);
}

export async function getArchive(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/user_archives");
  return instance(options);
}

export async function editArchive(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/edit_archives");
  return instance(options);
}

export async function getIndustry() {
  const options = buildOptions("", "legal/im.ChatGroup/industry");
  return instance(options);
}

export async function getGeneralReply(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/skills_list");
  return instance(options);
}

export async function addCustomReply(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/add_skills");
  return instance(options);
}

export async function delCustomReply(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/del_skills");
  return instance(options);
}

export async function getKnowledge(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/knowledge");
  return instance(options);
}

export async function getKnowledgeCate(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/cate_list");
  return instance(options);
}

export async function getWorkOrderCate(data: any) {
  const options = buildOptions(data, "legal/im.work_order/order_cate");
  return instance(options);
}

export async function addWorkOrder(data: any) {
  const options = buildOptions(data, "legal/im.work_order/add_order");
  return instance(options);
}

export async function getWorkOrder(data: any) {
  const options = buildOptions(data, "legal/im.work_order/get_order_detail");
  return instance(options);
}

export async function getWorkOrderDraft(data: any) {
  const options = buildOptions(data, "legal/im.work_order/get_draft_list");
  return instance(options);
}

export async function delWorkOrderDraft(data: any) {
  const options = buildOptions(data, "legal/im.work_order/del_draft_order");
  return instance(options);
}

export async function WorkOrderOperation(data: any) {
  const options = buildOptions(data, "legal/im.work_order/get_order_operation");
  return instance(options);
}

export async function WorkOrderHandle(data: any) {
  const options = buildOptions(data, "legal/im.work_order/get_order_handle");
  return instance(options);
}

export async function GetWorkOrderList(data: any) {
  const options = buildOptions(data, "legal/im.work_order/list", "GET");
  return instance(options);
}

export async function GetComplaintList(data: any) {
  const options = buildOptions(data, "legal/im.chat_group/com_list", "GET");
  return instance(options);
}

export async function GetEvaluateList(data: any) {
  const options = buildOptions(data, "legal/im.chat_group/eva_list", "GET");
  return instance(options);
}

export async function AddSupplement(data: any) {
  const options = buildOptions(data, "legal/im.work_order/supple_order");
  return instance(options);
}

export async function GetChatHistoryList(data: any) {
  const options = buildOptions(data, "legal/im.chat_group/log_list", "GET");
  return instance(options);
}

export async function ChangePwd(data: any) {
  const options = buildOptions(data, "legal/im.ChatGroup/change_password");
  return instance(options);
}

export async function UserEva(data: any) {
  const options = buildOptions(data, "legal/im.user/eva");
  return instance(options);
}

export async function GetLegalInfo(data: any) {
  const options = buildOptions(data, "legal/im.user/legal_info");
  return instance(options);
}

export async function AddComplaint(data: any) {
  const options = buildOptions(data, "legal/im.user/complaint");
  return instance(options);
}

export async function GetGroupVip(data: any) {
  const options = buildOptions(
    data,
    "legal/im.rights/user_package_rights",
    "GET"
  );
  return instance(options);
}

export function buildOptions(data: any, url: string, method?: string) {
  const options: any = {
    method: method || "POST",
    url,
  };
  if (options.method === "GET") {
    options.params = data;
  } else {
    options.data = qs.stringify(data);
  }
  return options;
}

export default instance;
